﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookShop.BLL;
using BookShop.DTO;
using BookShop.Library.Common;
using BookShop.Library.Constant;
using PagedList;

namespace BookShop.Admin.Controllers
{
    public class RateCommentController : Controller
    {
        public ActionResult Add()
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.RateCommentWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(long bookId, long customerId, string content, string ratePoint)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.RateCommentWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var rateComment = new RateComment
            {
                BookId = bookId,
                CommentContent = content,
                CreateTime = DateTime.Now,
                CustomerId = customerId,
                Status = RateCommentStatus.Active
            };
            double newRatePoint;
            var isValidRatePoint = double.TryParse(ratePoint, NumberStyles.Number,
                CultureInfo.CreateSpecificCulture("en-US"), out newRatePoint);
            if (isValidRatePoint)
            {
                rateComment.RatePoint = newRatePoint;
            }
            var addResult = RateCommentService.Add(rateComment);
            if (addResult != null)
            {
                return RedirectToAction("Detail", new { rateCommentId = rateComment.Id });
            }
            TempData[AlertMessage.TempDataKey] = AlertMessage.ErrorOccurs;
            ViewBag.AddBookId = bookId;
            ViewBag.AddCustomerId = customerId;
            ViewBag.AddRatePoint = ratePoint.ToString(CultureInfo.CreateSpecificCulture("vi-VN"));
            ViewBag.AddContent = content ?? string.Empty;
            return View();
        }

        public ActionResult Delete(long rateCommentId)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.RateCommentWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var rateComment = RateCommentService.GetById(rateCommentId);
            if (rateComment == null)
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.NotFoundObject;
            }
            else
            {
                var deleteResult = RateCommentService.Delete(rateComment);
                if (!deleteResult)
                {
                    TempData[AlertMessage.TempDataKey] = AlertMessage.ErrorOccurs;
                }
            }
            return RedirectToAction("List");
        }

        public ActionResult Detail(long rateCommentId)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.RateCommentRead))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var rateComment = RateCommentService.GetById(rateCommentId);
            if (rateComment != null)
            {
                rateComment.CustomerName = (CustomerService.GetById(rateComment.CustomerId) ?? new Customer()).FullName;
                rateComment.BookTitle = (BookService.GetById(rateComment.BookId) ?? new Book()).Title;
                return View(rateComment);
            }
            TempData[AlertMessage.TempDataKey] = AlertMessage.NotFoundObject;
            return RedirectToAction("List");
        }

        public ActionResult Edit(long rateCommentId)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.RateCommentWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var rateComment = RateCommentService.GetById(rateCommentId);
            if (rateComment != null)
            {
                return View(rateComment);
            }
            TempData[AlertMessage.TempDataKey] = AlertMessage.NotFoundObject;
            return RedirectToAction("List");
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(long rateCommentId, long customerId, string content, string ratePoint, int status)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.RateCommentWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var rateComment = RateCommentService.GetById(rateCommentId);
            if (rateComment == null)
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.NotFoundObject;
                return RedirectToAction("List");
            }
            rateComment.CustomerId = customerId;
            rateComment.CommentContent = content;
            rateComment.Status = status;
            double newRatePoint;
            var isValidRatePoint = double.TryParse(ratePoint, NumberStyles.AllowDecimalPoint,
                CultureInfo.CreateSpecificCulture("en-US"), out newRatePoint);
            if (isValidRatePoint)
            {
                rateComment.RatePoint = newRatePoint;
            }
            var updateResult = RateCommentService.Update(rateComment);
            if (updateResult != null)
            {
                return RedirectToAction("Detail", new { rateCommentId });
            }
            TempData[AlertMessage.TempDataKey] = AlertMessage.ErrorOccurs;
            return View(rateComment);
        }

        [ValidateInput(false)]
        public ActionResult List(long? rateCommentId, long? bookId, long? customerId, string content, string fromCreateTime,
            string toCreateTime, string fromRatePoint, string toRatePoint, int? status, int page = 1, int pageSize = 20)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.RateCommentRead))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("Index", "Home");
            }
            int totalRow;
            var model = RateCommentService.Search(rateCommentId, bookId, customerId, content, fromCreateTime, toCreateTime,
                fromRatePoint, toRatePoint, status, page, pageSize, out totalRow);
            ViewBag.SearchId = rateCommentId.ToString();
            ViewBag.SearchBookId = bookId.ToString();
            ViewBag.SearchCustomerId = customerId.ToString();
            ViewBag.SearchContent = content ?? string.Empty;
            ViewBag.SearchFromCreateTime = fromCreateTime ?? string.Empty;
            ViewBag.SearchToCreateTime = toCreateTime ?? string.Empty;
            ViewBag.SearchFromRatePoint = fromRatePoint ?? string.Empty;
            ViewBag.SearchToRatePoint = toRatePoint ?? string.Empty;
            ViewBag.SearchStatus = status.ToString();
            ViewBag.PageList = new StaticPagedList<RateComment>(model, page, pageSize, totalRow);
            ViewBag.RecordsCount = "Tổng số bản ghi: " + totalRow;
            if (model.Count != 0)
            {
                foreach (var rateComment in model)
                {
                    rateComment.CustomerName = (CustomerService.GetById(rateComment.CustomerId) ?? new Customer()).FullName;
                    rateComment.BookTitle = (BookService.GetById(rateComment.BookId) ?? new Book()).Title;
                }
            }
            return View(model);
        }

        public JsonResult GetAllCustomer()
        {
            return Json(CustomerService.GetAll());
        }

        public JsonResult GetAllBook()
        {
            return Json(BookService.GetAll());
        }
    }
}
